// 数据处理类，可根据工程配置。
// Data processing class, can be configured according to the project.

import type {
  AjaxResult,
  RequestOptions,
  UniRequestFailResult,
  UniRequestSuccessResult,
} from '#/request';

import { UniRequest } from './uniRequest';

/** Uni Request 选项 */
export type UniRequestOptions = Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete'> &
  Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete'> &
  Omit<UniDownloadFileOption, 'success' | 'fail' | 'complete'> & {
    /** 参数 */
    params?: Record<string, any> | Array<Record<string, any>> | string;
    /** query 参数 */
    query?: Record<string, any>;
    /** 进度变化 */
    onProgress?: (progressEvent: OnProgressEvent) => void;
  };

/** 创建 Uni Request 选项 */
export type CreateUniRequestOptions = UniRequestOptions & {
  /** 身份验证键 */
  authenticationKey?: string;
  /** 身份验证方案 */
  authenticationScheme?: string;
  /** 请求转换器 */
  transform?: RequestTransform;
  /** 请求参数 */
  requestOptions?: RequestOptions;
};

/** 请求转换器 */
export abstract class RequestTransform {
  /** 在发送请求之前调用的函数，它可以根据需要修改请求配置。 */
  beforeRequestHook?: (
    config: UniRequestOptions,
    options: RequestOptions,
    instance: UniRequest,
    fnType: 'upload' | 'download' | 'request',
  ) => UniRequestOptions | Promise<UniRequestOptions>;

  /** 处理响应数据 */
  transformResponseHook?: (
    res: UniRequestSuccessResult<AjaxResult>,
    options: RequestOptions,
  ) => any | Promise<any>;

  /** 请求失败处理 */
  requestCatchHook?: (
    error: UniRequestFailResult<AjaxResult>,
    config: UniRequestOptions,
    options: RequestOptions,
    instance: UniRequest,
    fnType: 'upload' | 'download' | 'request',
  ) => any | Promise<any>;
}
